﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mrsoft.Update.Common
{
    /// <summary>
    /// 同文件存放的配置统一创建接口
    /// </summary>
    public class ConfigFactory
    {
        /// <summary>
        /// XML文件全路径
        /// </summary>
        private readonly string file;

        public ConfigFactory(string file)
        {
            this.file = file;
        }

        public Config<bool> CreateConfig(string confPath, bool defValue)
        {
            return new Config<bool>(file, confPath, defValue);
        }

        public Config<string> CreateConfig(string confPath, string defValue)
        {
            return new Config<string>(file, confPath, defValue);
        }

        public Config<int> CreateConfig(string confPath, int defValue)
        {
            return new Config<int>(file, confPath, defValue);
        }

        public Config<double> CreateConfig(string confPath, double defValue)
        {
            return new Config<double>(file, confPath, defValue);
        }

        public Config<float> CreateConfig(string confPath, float defValue)
        {
            return new Config<float>(file, confPath, defValue);
        }

        public Config<long> CreateConfig(string confPath, long defValue)
        {
            return new Config<long>(file, confPath, defValue);
        }

        /// <summary>
        /// 存储添加了绑定内容项目
        /// </summary>
        private static HashSet<Control> controls = new HashSet<Control>();


        public static void BindingControlRegister(Control control)
        {
            if (!controls.Contains(control))
                controls.Add(control);
        }


        /// <summary>
        /// 将所有的数据绑定项目取消掉
        /// </summary>
        public static void ClearDataBindings()
        {
            lock (controls)
            {
                foreach (Control control in controls)
                {
                    control.DataBindings.Clear();
                }
                controls.Clear();
            }
        }
    }
}
